package com.shopng.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.shopng.model.SecurityUser;
import com.shopng.model.User;
import com.shopng.service.UserService;

@Component
public class CustomUserDetailsService implements UserDetailsService
{
  @Autowired  //数据库服务类
  private UserService suserService;//code7
  
  @Override
  public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException
  {
    //SUser对应数据库中的用户表，是最终存储用户和密码的表，可自定义
    //本例使用SUser中的email作为用户名:
   User user = suserService.findUserByName(userName); //code8
   if (user == null) {
       throw new UsernameNotFoundException("UserName " + userName + " not found");
   }
   SecurityUser sUser = new SecurityUser(user);
   return  sUser;//code9
  }
  
}
